package io.renren.modules.project.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import io.renren.modules.oss.constant.UploadConstant;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserTokenEntity;
import io.renren.modules.sys.service.SysUserTokenService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.project.entity.PojectOperationEntity;
import io.renren.modules.project.service.PojectOperationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 项目工作项附件表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-10-31 20:20:02
 */
@RestController
@RequestMapping("/generator/pojectoperation")
public class PojectOperationController {
	@Autowired
	private PojectOperationService pojectOperationService;
	@Autowired
	private SysUserTokenService sysUserTokenService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:pojectoperation:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<PojectOperationEntity> pojectOperationList = pojectOperationService.queryList(query);
		int total = pojectOperationService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(pojectOperationList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("generator:pojectoperation:info")
	public R info(@PathVariable("id") String id){
		PojectOperationEntity pojectOperation = pojectOperationService.queryObject(id);
		
		return R.ok().put("pojectOperation", pojectOperation);
	}

	/**
	 * 根据任务工作项ID查询下面的附件
	 */
	@RequestMapping("/query/{id}")
	@RequiresPermissions("generator:pojectoperation:info")
	public R query(@PathVariable("id") String id, HttpServletRequest request){
		String getContextPath = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+getContextPath+"/";
		List<PojectOperationEntity> pojectOperation = pojectOperationService.query(id);
		SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		SysUserTokenEntity userToken = sysUserTokenService.queryByUserId(user.getUserId());
		String baseUrl = basePath + UploadConstant.IMAGES_ROUTE + "/";
		for (int i=0; i < pojectOperation.size(); i++) {
			String tempPath = StringUtils.substringAfterLast(pojectOperation.get(i).getAttachPath(), "/");
			String imageUrl = baseUrl + tempPath + "?token=" + userToken.getToken();
			pojectOperation.get(i).setAttachPath(imageUrl);
			System.out.println("*****imageUrl******:"+imageUrl);
		}
		return R.ok().put("pojectOperation", pojectOperation);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("generator:pojectoperation:save")
	public R save(@RequestBody PojectOperationEntity pojectOperation){
		pojectOperationService.save(pojectOperation);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("generator:pojectoperation:update")
	public R update(@RequestBody PojectOperationEntity pojectOperation){
		pojectOperationService.update(pojectOperation);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:pojectoperation:delete")
	public R delete(@RequestBody String[] ids){
		pojectOperationService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 读取附件
	 */
	@RequestMapping("/operation/{id}")
	public R operation(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
		PojectOperationEntity pojectOperation = pojectOperationService.queryObject(id);
		response.setContentType("image/png");
		OutputStream output = response.getOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(pojectOperation.getOperation());//获取实体类对应Byte
		int len;
		byte[] buf = new byte[1024];
		while ((len = in.read(buf)) != -1) {
			output.write(buf, 0, len);
		}
		output.flush();
		output.close();
		return R.ok().put("buf",buf);
	}
}
